package pt.isel.pdm.ouryamba;

import java.util.Date;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class StatusActivity extends BaseActivity implements OnClickListener, TextWatcher
{

	private Button submit;
	private EditText editText;
	private TextView text;
	public final static String ID = StatusActivity.class.getName();
	private Intent uploadIntent;
	
	
//Own methods

	/**
	 * Enable the clicks on the submit button.</br>
	 * 
	 * According to the boolean parameter it change the text of the button or not.
	 * 
	 * @param changeText Indicate whether the button text should be changed or not.
	 * */
	private void enableSubmit(boolean changeText) 
	{
		submit.setEnabled(true);
		if(changeText)
		{
			submit.setText(R.string.buttonUpdateFace);
		}
	}
	
	/**
	 * Disable the clicks on the submit button.</br>
	 * 
	 * According to the boolean parameter it change the text of the button or not.
	 * 
	 * @param changeText Indicate whether the button text should be changed or not.
	 * */
	private void disableSubmit(boolean changeText)
	{
		submit.setEnabled(false);
		if(changeText)
		{
			submit.setText(R.string.buttonBusyFace);
		}
	}
		
	
//Activity implementation	
	
	/**
	 * Initialize all the references for the views of these layout.</br>
	 * 
	 * @see #disableSubmit(boolean)
	 * */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_status);
        submit = (Button) findViewById(R.id.submit_BT);
        submit.setOnClickListener(this);
        editText = (EditText) findViewById(R.id.statusInput_ET);
        text = (TextView)findViewById(R.id.charsLeft_TV);
        editText.addTextChangedListener(this);
    }

    /**
     * Write the hint of the text view where the status will be written.
     * */
   	@Override
	public void onResume() 
	{
   		super.onResume();
   		if(editText.getText().length() == 0)
   		{
   			disableSubmit(false);
   		}
   		editText.setHint(getResources().getString(R.string.statusHint1) 
        		+ General.SPACE_STRING + app.getMaxChars() + General.SPACE_STRING
        		+ getResources().getString(R.string.statusHint2));
	}
   	
   	/**
   	 * Set some menu items invisible (the ones that don't matter for this Activity).</br>
   	 * 
   	 * Hide the status and the refresh menu item because there is none sense having them here.
   	 * */
   	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.shared_menu, menu);
		menu.getItem(2).setVisible(false);
		menu.getItem(3).setVisible(false);
		return true;
	}

	
//OnClickListener implementation
	

	/**
   	 * Send an intent to the service to publish the status.</br>
   	 * 
   	 * Once the submit button is clicked, it is disabled and the intent is prepared to be sent to
   	 * the StatusUploadService.</br>
   	 * 
   	 * The status is immediately stored in the database with the committed value as false.
   	 * 
   	 * @see General#dbInsert(long, String, String, String, boolean)
   	 * @see #disableSubmit(boolean)
   	 * */
	@Override
    public void onClick(View v) 
	{
    	if(uploadIntent == null)
		{
			uploadIntent = new Intent(getApplicationContext(), StatusUploadService.class);
		}
    	app.dbInsert(0, new Date().toString(), editText.getText().toString(), app.getUsername(), false);
    	startService(uploadIntent);
    	editText.setText(General.EMPTY_STRING);
    	disableSubmit(false);
	}
	

//TextWatcher implementation
	  
	/**
	 * Delegate the behavior when the status text is changed.
	 * Every time the message is changed, lot of things must be updated. The text view where the
	 * count of remain characters is shown, needs to be updated, increasing or decreasing the counter.
	 * The color of this counter values are also changed to red when the number of characters exceed
	 * the limit, and to black when the number of characters are below the limit. 
	 * The submit button should also be updated because when the length of the message is zero or 
	 * above the maximum number of characters allowed, this needed to be disabled so none invalid
	 * statuses are sent to the server.
	 * 
	 * Every time the status length is above the limit, a message is shown to user through a Toast
	 * object. 
	 * 
	 * @see #enableSubmit(boolean)
	 * @see #disableSubmit(boolean)
	 * */
	@Override
	public void onTextChanged(CharSequence arg0, int start, int before, int count)
	{	
		if(arg0.length() == 0)
		{
			text.setText(General.EMPTY_STRING);
			disableSubmit(false);
		}
		else
		{
			text.setText(String.valueOf(app.getMaxChars() - arg0.length()));
			enableSubmit(false);
		}
		if(arg0.length() > app.getMaxChars())
		{
			text.setTextColor(android.graphics.Color.RED);
			disableSubmit(false);
			Toast.makeText(this, R.string.maxCharsAccomplished, Toast.LENGTH_LONG).show();
		}
		else
		{
			text.setTextColor(android.graphics.Color.GREEN);
			enableSubmit(false);
		}	
	}

	/**
	 * Don't perform nothing but needs to be declared anyway because of this class implementation.
	 * */
	@Override
	public void afterTextChanged(Editable arg0) { /* DO NOTHING!*/ }
	
	/**
	 * Don't perform nothing but needs to be declared anyway because of this class implementation.
	 * */
	@Override
	public void beforeTextChanged(CharSequence arg0, int start, int count, int after) { /* DO NOTHING!*/ }
	
}
